package gang.org.springframework.framework.support;

import gang.org.springframework.framework.beanpostprocessor.GangBeanPostProcessor;
import gang.org.springframework.framework.context.GangApplicationContextAware;
import gang.org.springframework.framework.context.GangConfigurableApplicationContext;
import gang.org.springframework.framework.context.GangResourceLoaderAware;

import java.security.AccessControlContext;

/**
 * 类说明：gang.chen springboot深度实践
 *
 * @author gang.chen
 * @since 2022/12/27
 */
public class GangApplicationContextAwareProcessor implements GangBeanPostProcessor {

    private final GangConfigurableApplicationContext applicationContext;

    private final Object embeddedValueResolver;

    public GangApplicationContextAwareProcessor(GangConfigurableApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        this.embeddedValueResolver = null;
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) {
        //场景一 TODO
        if(!(bean instanceof GangResourceLoaderAware || bean instanceof GangApplicationContextAware )){
            return bean;
        }

        //场景二 TODO
        AccessControlContext acc = null;

        //场景三 TODO
        if (acc != null){
            //
        }else {
            invokeAwareInterface(bean);
        }

        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) {
        //TODO
        return null;
    }

    private void invokeAwareInterface(Object bean){

        //场景一 TODO

        //场景二 TODO

        //场景三
        if (bean instanceof GangResourceLoaderAware){
            ((GangResourceLoaderAware)bean).setResourceLoader(this.applicationContext);
        }

        //场景四 TODO

        //场景五 TODO

        //场景六 TODO

        //场景七 TODO
        if (bean instanceof GangApplicationContextAware){
            ((GangApplicationContextAware)bean).setApplicationContext(this.applicationContext);
        }
        
    }
}
